package com.model.common.utils.always;

import android.widget.EditText;

import java.text.DecimalFormat;

/**
 * Create by Carson on 2021/12/22.
 * 关于文本类的封装
 */
public class StringUtils {

    //获取输入框内容
    public static String getEditString(EditText view) {
        if (view != null) {
            return view.getText().toString().trim();
        } else {
            return "";
        }
    }

    //检查字符串是否为空
    public static boolean isEmpty(String str) {
        return str == null || str.length() == 0 ||
                str.equalsIgnoreCase("null") || str.trim().equals("");
    }

    /**
     * 保留两位小数
     */
    public static String formatDouble(double number, boolean isSymbol) {
        if (number == 0) {
            return "0";
        }
        DecimalFormat format = new DecimalFormat("######0.00");
        String unit = "";
        if (isSymbol) {
            unit = "¥";
        }
        if (isIntegerForDouble(number)) {
            int system = Double.valueOf(number).intValue();
            return unit + system;
        } else {
            return unit + format.format(number);
        }
    }

    /**
     * 判断double类型是否可以取整
     */
    private static boolean isIntegerForDouble(double number) {
        double eps = 1e-10;//精度范围
        return number - Math.floor(number) < eps;
    }

    /**
     * 富文本适配
     */
    public static String getHtmlData(String bodyHTML) {
        String head = "<head>"
                + "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\"> "
                + "<style>img{max-width: 100%; width:auto; height:auto;}</style>"
                + "</head>";
        return "<html>" + head + "<body>" + bodyHTML + "</body></html>";
    }

    public static String avoidNull(String str) {
        return str == null ? "" : str;
    }
}